package com.cloudlead.plugin.security.action;

import com.cloudlead.common.lang.annotation.Label;
import com.cloudlead.common.lang.persistence.OnlineUser;
import com.cloudlead.common.lang.security.LoginSource;
import com.cloudlead.common.lang.security.Principal;
import com.cloudlead.common.lang.security.SecurityUtils;
import com.cloudlead.common.lang.security.Subject;
import com.cloudlead.common.worker.action.impl.AbstractActionFactory;
import com.cloudlead.common.worker.annotation.Action;
import com.cloudlead.common.worker.annotation.Order;
import com.cloudlead.common.worker.security.ScAuth;
import com.cloudlead.common.worker.security.annotation.RequireGuest;
import com.cloudlead.plugin.security.BusinessModules;
import com.cloudlead.plugin.security.service.AuthorizationService;
import org.springframework.stereotype.Controller;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * Created by dean on 2015/8/2.
 */
@Controller("authorizationActionFactory")
@Label("安全认证管理")
public class AuthorizationActionFactory extends AbstractActionFactory {
    public AuthorizationActionFactory() {
        super(BusinessModules.Authorization);
    }

    @Inject
    private AuthorizationService authorizationService;

    @Action
    @Order(1)
    @RequireGuest
    @Label("获取令牌")
    public ScAuth getToken(@NotNull @Label("用户名") String username,
                           @NotNull @Label("密码") String password, @NotNull @Label("登录源") LoginSource source, @Label("用户组") String group, @Label("验证码") String code) {
        return authorizationService.getToken(username, password, source, group, code);
    }

    @Action
    @Order(2)
    @Label("切换用户组")
    public void changeGroup(@Label("用户组") String group) {
        authorizationService.changeGroup(group);
    }

    @Action
    @Order(3)
    @Label("移除令牌")
    public void removeToken() {
        authorizationService.removeToken();
    }

    @Action
    @Order(4)
    @RequireGuest
    @Label("忘记密码")
    public void forgetPassword(@NotNull @Label("手机号码") String mobile, @NotNull @Label("验证码") String code,
                               @NotNull @Label("密码") String password) {
        authorizationService.forgetPassword(mobile, code, password);
    }

    @Action
    @Order(5)
    @RequireGuest
    @Label("获取短信验证码")
    public String getVerifyCode(@NotNull @Label("手机号码") String mobile) {
        return authorizationService.getVerifyCode(mobile);
    }

    @Action
    @Order(6)
    @Label("查询当前用户信息")
    public Principal findCurrentUserInf() {
        Subject subject = SecurityUtils.getSubject();
        if (null == subject) {
            return null;
        }
        return subject.getPrincipal();
    }
}
